<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/30
 * Time: 8:40
 */

namespace app\qgroup\service;


use app\common\service\SysConfigService;
use think\Exception;

class TulingService
{
    /**
     * @param $userid int 用户ID
     * @param $type int 输入类型:0-文本(默认)、1-图片、2-音频
     * @param $msg string 文本信息或图片链接地址或录音链接地址
     * @param $city string 用户所在城市
     * @param string $province 用户所在省份
     * @param string $street 用户所在街道
     * @return array
     */
    public function getReturnData($userid , $type , $msg , $city , $province = '' , $street = '')
    {
        return [
            'reqType'=>$type           #输入类型:0-文本(默认)、1-图片、2-音频
            ,'perception'=>[
                'inputText'=>[
                    'text'=> $type == 0 ? $msg : ''
                ]
                ,'inputImage'=>[
                    'url'=> $type == 1 ? $msg : ''
                ]
                ,'inputMedia'=>[
                    'url'=> $type == 2 ? $msg : ''
                ]
                ,'selfInfo'=>[
                    'location'=>[
                        'city'=>$city
                        ,'province'=>$province
                        ,'street'=>$street
                    ]
                ]
            ]
            ,'userInfo'=>[
                'apiKey'=>'5ce72254c410d9d1907cb2fe525e91ce'
                ,'userId'=>$userid
            ]
        ];
    }

    /**
     * @param $data string JSON格式数据
     * @param null|string $url 访问URL
     * @return mixed
     */
    public function sendMessage($data,$url = null){
        $url = $url?:SysConfigService::getURLConfig('A_TULING');
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl, CURLOPT_URL, $url);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }

    /**
     * 是否存在异常
     * @param $code
     * @return bool
     * @throws Exception
     */
    public function isException($code)
    {
        $exceptions = array(
            '5000'=>	'无解析结果'
            ,'6000'=>	'暂不支持该功能'
            ,'4000'=>	'请求参数格式错误'
            ,'4001'=>	'加密方式错误'
            ,'4002'=>	'无功能权限'
            ,'4003'=>	'该apikey没有可用请求次数'
            ,'4005'=>	'无功能权限'
            ,'4007'=>	'apikey不合法'
            ,'4100'=>	'userid获取失败'
            ,'4200'=>	'上传格式错误'
            ,'4300'=>	'批量操作超过限制'
            ,'4400'=>	'没有上传合法userid'
            ,'4500'=>	'userid申请个数超过限制'
            ,'4600'=>	'输入内容为空'
            ,'4602'=>	'输入文本内容超长(上限150)'
            ,'7002'=>	'上传信息失败'
            ,'8008'=>	'服务器错误'
        );
        foreach ($exceptions as $key => $msg){
            if ($code == $key){
                throw new Exception($msg,$key);
            }
        }
        return false;
    }
}